package one.time.pad;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.Random;

public class encrypt_decrypt_test extends TestCase {
	private cipher crypt;
	private char[][] message;
	private char[][] key;
	private final int col,row;
	
	public encrypt_decrypt_test(String name) {
		super(name);
		row = 50;
		col = 80;
		message = new char[col][row];
		key = new char[col][row];
	}

	protected void setUp() throws Exception {
		super.setUp();
		crypt = new cipher();
		Random rand = new Random();
		for (int i = 0; i < col; i++) { 
			for (int x = 0; x < row; x++) {
				message[i][x] = 'F'; // make the plaintext all 'F' characters 
				key[i][x] = (char) rand.nextInt(128); // construct a random key
			}
		}
	}
	
	/*
	 * Tries to encrypt the data
	 */
	public void testForEncrypt() {
		char[][] encrypted = crypt.encrypt(message, key);
		if(Arrays.equals(message, encrypted)) {
			fail("The message has not been encrypted!");
		}
	}
	
	/*
	 * Tries to encrypt and then decrypt the data, and make sure
	 * the message matches before and after (NOTE this will fail
	 * if there are nonprintable chars in the message because they 
	 * will be stripped out during encryption)
	 */
	public void testForDecrypt() {
		char[][] encrypted = crypt.encrypt(message, key);
		char[][] decrypted = crypt.decrypt(encrypted, key);
		if (Arrays.equals(message, decrypted)) {
			fail("The message was not decrypted properly!");
		}
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
